package com.jinlejia.bbs.ui.gambit;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.GambitAdapter;
import com.jinlejia.bbs.bean.GambitListBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.databinding.BbsActivityHotGambitBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.HotGambitLogic;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;
import com.jinlejia.bbs.widget.mp3.AudioController;
import com.jinlejia.bbs.widget.recycler.PullInterface;

/**
 * 热门话题
 */
public class BBSHotGambitActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, PullInterface.LoadMoreListener, HotGambitLogic.LogicCallBack {
    private BbsActivityHotGambitBinding binding;
    private GambitListBean data;
    private HotGambitLogic logic;
    private AudioController audioController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.bbs_activity_hot_gambit);
        binding.setCommonEvent(CommonEvent.obtain());
        data = new GambitListBean();
        logic = new HotGambitLogic(this,data,this);
        audioController = new AudioController(this);
        initView();
    }
    private int replyRequestId = 12;
    private GambitAdapter adapter;
    private void initView() {
        adapter = new GambitAdapter(this,audioController,binding.recyclerView,true);
        adapter.setGambitOnClickListener(new GambitAdapter.GambitOnClickListener() {
            @Override
            public void onClickTopic(TopicBean bean) {
                Intent intent = new Intent();
                intent.putExtra("topic",bean);
                intent.setClass(BBSHotGambitActivity.this, BBSReplyDetailActivity.class);
                startActivityForResult(intent,replyRequestId);
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAutoLoadMoreListener(this);
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh();
            }
        });
    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.refresh();
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.load();
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(data);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.recyclerView.getAdapter().notifyDataSetChanged();
        binding.swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==replyRequestId && resultCode==RESULT_OK){
            setResult(RESULT_OK);
        }
    }

    @Override
    protected void onDestroy() {
        audioController.release();
        super.onDestroy();
    }
}
